package model.components;

import java.awt.Rectangle;

import model.ComponentTypeEnum;
import model.GameContext;
import model.LayerEnum;
import model.ModelComponent;
import model.characters.CharacterInterface;
import model.characters.NPC;

/**
 *
 * @author thiago.barreto
 *	Classe Utilitaria
 */
public class ScreenComponentUtil {

	public static void update(CharacterInterface c){
		changeLifeBar(c);
	}

	public static ScreenComponent getNewLifeBar(CharacterInterface c, Rectangle region){
		int currentLife = c.getLife();
		if(isPlayer(c)){
			if(currentLife > 250)
				return new ScreenComponent(ScreenComponentEnum.FIVE_LIFE_BAR, region,LayerEnum.LAYER_1, c);
			if(currentLife > 210)
				return new ScreenComponent(ScreenComponentEnum.FOUR_LIFE_BAR, region,LayerEnum.LAYER_1, c);
			if(currentLife > 130)
				return new ScreenComponent(ScreenComponentEnum.THREE_LIFE_BAR, region,LayerEnum.LAYER_1, c);
			if(currentLife > 70)
				return new ScreenComponent(ScreenComponentEnum.TWO_LIFE_BAR, region,LayerEnum.LAYER_1, c);
			if(currentLife > 1)
				return new ScreenComponent(ScreenComponentEnum.ONE_LIFE_BAR, region,LayerEnum.LAYER_1, c);
			return new ScreenComponent(ScreenComponentEnum.EMPTY_LIFE_BAR, region,LayerEnum.LAYER_1, c);
		}else{
			if(currentLife >= 60)
				return new ScreenComponent(ScreenComponentEnum.NPC_FIVE_LIFE_BAR, region,LayerEnum.LAYER_1, c);
			if(currentLife >= 50)
				return new ScreenComponent(ScreenComponentEnum.NPC_FOUR_LIFE_BAR, region,LayerEnum.LAYER_1, c);
			if(currentLife >= 40)
				return new ScreenComponent(ScreenComponentEnum.NPC_THREE_LIFE_BAR, region,LayerEnum.LAYER_1, c);
			if(currentLife >= 30)
				return new ScreenComponent(ScreenComponentEnum.NPC_TWO_LIFE_BAR, region,LayerEnum.LAYER_1, c);
			if(currentLife > 1)
				return new ScreenComponent(ScreenComponentEnum.NPC_ONE_LIFE_BAR, region,LayerEnum.LAYER_1, c);
			return new ScreenComponent(ScreenComponentEnum.NPC_EMPTY_LIFE_BAR, region,LayerEnum.LAYER_1, c);
		}
		
	}

	public static void changeLifeBar(CharacterInterface c){
		ModelComponent lifeBarComponent = null;
		// muda barra do jogador
		if(isPlayer(c)){
			for(ModelComponent k : GameContext.getCurrentStage().getModelComponents()){
				if(k instanceof ScreenComponent && k.getRegion().equals(GameContext.PLAYER_LIFE_BAR_RECTANGLE)){
					lifeBarComponent = k;
				}
			}
			GameContext.getCurrentStage().getModelComponents().remove(lifeBarComponent);
			ModelComponent newLifeBar = getNewLifeBar(c, GameContext.PLAYER_LIFE_BAR_RECTANGLE);
			GameContext.getCurrentStage().getModelComponents().add(newLifeBar);
		}else{
			// muda barra do inimigo
			
			int contadorInimigos = retrieveEnemyNumber();

			//novo retangulo com a posicao atual do npc
			Rectangle npcLifeBarRectangle = new Rectangle(c.getRegion().x, c.getRegion().y - 50, GameContext.NPC_DEFAULT_RECTANGLE_WIDTH, GameContext.NPC_DEFAULT_RECTANGLE_HEIGHT);
			
			for(ModelComponent k : GameContext.getCurrentStage().getModelComponents()){
				if(k instanceof ScreenComponent){
					ScreenComponent sc = (ScreenComponent)k;
					if(sc.getCharacter().equals(c))
						lifeBarComponent = k;
				}
			}
			
			if(lifeBarComponent != null){
				GameContext.getCurrentStage().getModelComponents().removeAll(GameContext.npcLifeBarRectangleList);
				GameContext.getCurrentStage().getModelComponents().remove(lifeBarComponent);
				GameContext.npcLifeBarRectangleList.remove(lifeBarComponent);
			}
			
			if(GameContext.npcLifeBarRectangleList.size() < contadorInimigos){
				GameContext.npcLifeBarRectangleList.add(getNewLifeBar(c, npcLifeBarRectangle));
			}
			
			GameContext.getCurrentStage().getModelComponents().addAll(GameContext.npcLifeBarRectangleList);
			
		}
	}

	public static int retrieveEnemyNumber(){
		int contadorInimigos = 0;
		for(CharacterInterface character : GameContext.getCurrentStage().getCharacters()){
			if(character instanceof NPC)
				contadorInimigos++;
		}
		return contadorInimigos;
	}
	
	public static boolean isPlayer(CharacterInterface c){
		return c.getType().equals(ComponentTypeEnum.PLAYER);
	}


}
